package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Customer;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;
import com.by.common.group.V;
import com.by.query.CustomerQuery;
import com.by.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 江俊俊
 * @version 1.0
 * @description
 * @date 2025/2/14 14:43
 * @since JDK1.8
 */
@RestController
@RequestMapping("/api")
@Api(tags = "客户管理")
public class CustomerController extends BaseController {

    @Autowired
    CustomerService customerService;

    @GetMapping("/customer")
    @ApiOperation("查询用户")
    public ResultBean get(Integer id){
        Customer customer = customerService.get(id);
        return success("客户信息",customer);
    }

    @PostMapping("/customer")
    @ApiOperation("保存用户")
    public ResultBean save(@Validated(ISave.class) @RequestBody Customer customer, BindingResult result){
        V.validate(result);
        customerService.save(customer);
        return success("保存成功");
    }

    @PostMapping("/customer/list")
    @ApiOperation("查询客户列表")
    public ResultBean<PageBean<List<Customer>>> query(@RequestBody CustomerQuery customerQuery){
        PageBean pageBean = customerService.queryByMap(BeanUtil.beanToMap(customerQuery));
        return success("客户列表",pageBean);
    }
}
